package com.example.bootintegration.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author Quintoy
 * @date 2024/11/28
 */
@Component
public class MQRedis {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void send(String msg){
        redisTemplate.convertAndSend("demo-channel", msg);
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisListener redisListener) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(redisListener, new PatternTopic("demo-channel"));

        return container;

    }
}
